package com.css.zfzx.sjcj.modules.yhdisasiterschedule.dao.primary.impl;

import com.css.zfzx.sjcj.modules.fxpc.fxCasEMap.repository.impl.FxCasEMapNativeRepositoryImpl;
import com.css.zfzx.sjcj.modules.pojo.QueryVo;
import com.css.zfzx.sjcj.modules.yhdisasiterschedule.dao.primary.YhDisasterDao;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * @Description: pc-statistics
 * @Author: lhl
 * @CreateDate: 2021/8/31 11:11
 */
@Repository
public class YhDisasterDaoImpl implements YhDisasterDao {
    @PersistenceContext
    private EntityManager em;

    private static final Logger log = LoggerFactory.getLogger(YhDisasterDaoImpl.class);

    //住建select province,extends1,count(*) from yh_disaster
    // where extends1 is not null
    // group by province,extends1
    @Override
    public List<Object[]> count1(int identify) {
        StringBuilder sql = new StringBuilder("select ");
        if (identify==1){
            sql.append(" province");
        }else if (identify==2){
            sql.append(" area");
        }
        sql.append(" ,extends1,count(*) from yh_disaster");
        StringBuilder whereSql = new StringBuilder(" where extends1 is not null ");
        // 删除标识为1：有效
        whereSql.append(" and is_valid = 1");
        if (identify==1){
            whereSql.append(" group by province,extends1");
        }else if (identify==2){
            whereSql.append(" group by area,extends1");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString());
        return query.getResultList();
    }

    @Override
    public List<Object[]> count2(int identify) {
        StringBuilder sql = new StringBuilder("select ");
        if (identify==1){
            sql.append(" province");
        }else if (identify==2){
            sql.append(" area");
        }
        sql.append(",yh_project_type,count(*) from yh_disaster");
        StringBuilder whereSql = new StringBuilder(" where is_valid = 1 ");
        if (identify==1){
            whereSql.append(" group by province,yh_project_type");
        }else if (identify==2){
            whereSql.append(" group by area,yh_project_type");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString());
        return query.getResultList();
    }
}
